/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agents;

import env.Action;
import env.Orientation;
import env.Enviroment;
import env.TileType;
import java.awt.Color;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 * @author orcas
 */
public class GreedyAgent extends Agent {
    static Icon iconNorth=new ImageIcon("images/AgentNorthBlue.gif");
    static Icon iconSouth=new ImageIcon("images/AgentSouthBlue.gif");
    static Icon iconEast=new ImageIcon("images/AgentEastBlue.gif");
    static Icon iconWest=new ImageIcon("images/AgentWestBlue.gif");

    int targetR, targetC;
    boolean hasTarget;

    public GreedyAgent(String name, int row, int col, Orientation orientation, Color color) {
        super(name, row, col, orientation, color);
        hasTarget = false;
    }

    public Action getMove() {
        Action move = null;
        
        //can't turn on ice - only option is fwd!
        if(getCurrentTile().isIce())
            return Action.fwd;
        
        if(!getEnv().map[targetR][targetC].hasFlag())
            hasTarget=false;
        if (!hasTarget) {
            findTarget(getEnv());
        }
        Orientation orientation = getOrientation();
        switch (orientation) {
            case North: {
                if (targetR < getRow() && getEnv().isCellFree(getRow() -1, getCol() )) {
                    move =Action.fwd; //this is a good direction
                } 
                else if (targetC < getCol()) {
                    move = Action.left;
                } else {//if(targetC > getCol())
                    move = Action.right;
                    }
                break;
            }
            case South: {
                        if (targetR > getRow() && getEnv().isCellFree(getRow() +1, getCol())) {
                move = Action.fwd; //this is a good direction
            } else if (targetC < getCol()) {
                move = Action.right;
            } else {
                move = Action.left;
            }
                break;
            }
            case West: {
                        if (targetC  < getCol() && getEnv().isCellFree(getRow() , getCol()-1)) {
                move = Action.fwd; //this is a good direction
            } else if (targetR < getRow()) {
                move = Action.right;
            } else {
                move = Action.left;
            }
                break;
            }
            case East: {
                        if (targetC > getCol() && getEnv().isCellFree(getRow() , getCol()+1)) {
                move = Action.fwd; //this is a good direction
            } else if (targetR < getRow()) {
                move = Action.left;
            } else {
                move = Action.right;
            }
                break;
            }
        }
        //System.out.println(getName()+"'s next move is "+move);
        return move;
    }
    

    private int abs(int x) {
        return x>=0?x:-x;
    }
    
    private void findTarget(Enviroment env) {
        int targetD=Integer.MAX_VALUE;
        for (int i = 0; i < env.rows; i++) {
                for (int j = 0; j < env.cols; j++) {
                    if (env.map[i][j].hasFlag() && abs(getRow()-i) + abs(getCol()-j) < targetD) {
                        targetR=i;
                        targetC=j;
                        targetD=abs(getRow()-i) + abs(getCol()-j);
                        hasTarget=true;
                    }
                }
            }
        if(hasTarget) {
            System.out.println("$$$ "+getName()+"'s new target is the flag at "+targetR+","+targetC+" $$$");
            env.drawArrow(getRow(), getCol(), targetR, targetC, getColor());
        }
        else 
            System.out.println("$$$ "+getName()+" has no targer! $$$");
    }

    @Override
    public Icon getIcon() {
        switch(this.getOrientation()) {
            case North: return iconNorth;
            case East: return iconEast;
            case West: return iconWest;
            case South: return iconSouth;
        }
        return null;
    }
}
